package com.gf.fund.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gf.base.controller.BasicController;
import com.gf.common.bean.DataResponse;
import com.gf.common.bean.Page;
import com.gf.common.constant.Constants;
import com.gf.common.constant.ViewPaths;
import com.gf.common.staticer.MessageRemainsHelper;
import com.gf.common.util.WebHelper;
import com.gf.fund.service.FundService;
import com.gf.user.model.User;

/**
 * 
 * @author zhuguifeng
 * 
 */
@Controller
@RequestMapping(value = "/fund")
public class FundController extends BasicController {
	@Autowired
	private FundService fundService;

	/**
	 * 资金主页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView fundIndex() {
		User user = WebHelper.getSessionUser();
		int messageCount = MessageRemainsHelper.getRemains(user.getId());

		ModelAndView mvc = new ModelAndView();

		mvc.addObject("user", user);
		mvc.addObject("messageCount", messageCount);
		mvc.setViewName(ViewPaths.FUND_INDEX);

		return mvc;
	}

	/**
	 * 充值
	 * 
	 * @param userId
	 * @param operate
	 */
	@RequestMapping(value = "charge", method = RequestMethod.POST)
	public @ResponseBody
	void charge(long userId, double operate) {
		double total = fundService.charge(userId, operate);
	}

	/**
	 * 消费
	 * 
	 * @param userId
	 * @param operate
	 * @param event
	 */
	public @ResponseBody
	void consume(long userId, double operate, String event) {
		double total = fundService.consume(userId, operate, event);
	}

	/**
	 * 消费记录
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/consumeList", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse consumeList(int currentPage, int pageSize, long startTime,
			long endTime) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return null;
		}

		Page page = fundService.getConsumeFundByUserId(user.getId(), pageSize,
				currentPage, startTime, endTime);

		DataResponse dataResponse = new DataResponse();
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		
		return dataResponse;
	}

	/**
	 * 充值记录
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/chargeList", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse chargeList(int currentPage, int pageSize, long startTime,
			long endTime) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return null;
		}

		Page page = fundService.getChargeFundByUserId(user.getId(), pageSize,
				currentPage, startTime, endTime);

		DataResponse dataResponse = new DataResponse();
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;
	}
}
